package model

import (
	"time"
	"errors"
)

type Paper struct {
	Id         int64
	MinLevel   int32 // user level limit
	Name       string
	Enable     bool
	CreatedAt  time.Time
	UpdatedAt  time.Time
	TotalScore int32
	PassScore  int32
	AvgScore   int32 // average score for user
	Duration   int32
	Describe   string
	Sids       []int64
	Sections   []*Section `xorm:"-"`
}

func (p *Paper) OneById() error {
	ok, err := orm.ID(p.Id).Get(p)
	if !ok {
		return errors.New(PaperNotFound)
	}
	return err
}

func (p *Paper) Create() error {
	p.Enable = true
	ok, err := orm.Insert(p)
	if ok == 0 {
		return errors.New(PaperCreateFailed)
	}
	return err
}

func (p *Paper) Update() error {
	ok, err := orm.Id(p.Id).Cols("sids").Update(p)
	if ok == 0 {
		return errors.New(PaperUpdateFailed)
	}
	return err
}
